<?php
// @formatter:off
/**
 * @file c.users.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.users.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/themes/default/controlers/contents/c.users.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_USER_LIST)) {
	require_once "{$this->_themeRoot}/controlers/generics/bundlepreview.php";
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcThemeAssigns;
	global $wcAction;
	global $wcUsersHolder;
	global $wctDefaults;
	global $wcPageCache;
	global $wcGroup;

	$wcPageCache->disabled();

	$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	$minTagInPage = $wctDefaults["max-per-page"]["users"] * ($page - 1);
	$maxTagInPage = $minTagInPage + $wctDefaults["max-per-page"]["users"];

	$wcThemeAssigns["PATH"] = "All Users (page {$page})";
	$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.users.html";

	//
	// Selecting only users in range.
	$users = array();
	$userCount = 0;
	foreach($wcUsersHolder->itemIds() as $id) {
		if($userCount >= $minTagInPage && $userCount < $maxTagInPage) {
			$user = $wcUsersHolder->item($id);
			if($user && $user->ok()) {
				$users[] = $user;
			} else {
				$userCount--;
			}
		}
		$userCount++;
	}

	wcSetPager(requestParams(true, $wcAction, array('page')), ceil($userCount / $wctDefaults["max-per-page"]["users"]), $page);

	//
	// Creating multi-array by prefix
	$lists = array();
	$ids = 1;
	foreach($users as $user) {
		$name = $user->name();
		$prefix = strtoupper(isset($name[0]) ? $name[0] : "#");
		if($prefix < 'A' || $prefix > 'Z') {
			$prefix = '#';
		}
		$prefix = "{$prefix}";
		if(!isset($lists[$prefix])) {
			$lists[$prefix] = array(
				"prefix" => $prefix,
				"list" => array()
			);
		}

		$lists[$prefix]["list"][] = wcGetUserPreviewAssigns($user);
	}
	ksort($lists);

	$wcThemeAssigns["USERS-LISTS"] = $lists;
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to list users";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>